<?php 
/**
 * alltosun.com 影院订单列表
 * ============================================================================
 * 版权所有 (C) 2009-2011 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 高昂 (gaoa@alltosun.com) $
 * $Date: 2016-5-9 下午12:03:55 $
 * $Id: $
 */

class Action
{
    private $per_page = 50;
    
    /**
     * 显示订单列表
     */
    public function index()
    {
        $page          = Request::Get('page_no', 1);
        $status        = Request::getParam('status', 1);
        $progress      = Request::getParam('progress', '-1');
        $task_user_id  = Request::getParam('task_user_id', '-1');
        $type          = Request::getParam('type', 5);
        $search_filter = Request::getParam('search_filter', array());
        $is_excel      = Request::getParam('is_excel', 0);
        $test          = Request::getParam('test', 0);
        $store_type    = Request::getParam('store_type', 100);
    
        // 拼装搜索条件
        $is_empty = 0;
        $filter = array();
        if (isset($search_filter['id']) && $search_filter['id']) {
            $filter['id'] = $search_filter['id'];
        }
        if ($store_type != -1) {
            $order_branch_ids = _model('order_branch')->getFields('order_id', array('store_type'=>$store_type));
            $filter['id'] = $order_branch_ids;
        }
    
        if (isset($search_filter['progress']) && $search_filter['progress'] && $search_filter['progress'] != '-1') {
            $filter['progress'] = $search_filter['progress'];
        }
        if (isset($search_filter['order_code']) && $search_filter['order_code']) {
            $decoded_id = order_helper::order_id_decode($search_filter['order_code']);
            if(is_numeric($decoded_id)) {
                $filter['id'] = $decoded_id;
            }
        }
        // 获取当前登录人的角色
        $role_id = user_helper::get_admin_role_id();
        if (!user_helper::is_all_can_show($role_id)) {
            $stype = 0;
            if ($store_type != -1){
                $stype = array($store_type);
            }
            $filter['store_id'] = store_helper::get_store_ids_by_admin_user($stype);
            if(!$filter['store_id']){
                $filter['store_id'] = -1;
            }
        }

//        $role_id = user_helper::get_admin_role_id();
//        $cur_admin_id = user_helper::get_admin_id();
//        if (user_config::ASSISTANT == $role_id) {
//            // 获取此助理的所有合伙人的订单
//            $dealer_ids = user_helper::get_assistant_dealer_ids();
//            if ($dealer_ids && is_array($dealer_ids)) {
//                $filter['agency_id'] = $dealer_ids;
//            }
//        } elseif (user_config::MULTIPLE_DIVISION == $role_id) {
//            $province_codes = _model('multiple_area')->getFields('province_code', array('user_id'=>$cur_admin_id, 'status' => 1));
//            if ($province_codes) {
//                $address_dealer_ids = _model('dealer_address')->getFields('user_id', array('province_code' => $province_codes));
//                if ($address_dealer_ids) {
//                    $store_ids = _model('store')->getFields('id', array('is_enable'=>1, 'is_delete' => 1, 'agency_id' => $address_dealer_ids));
//                    $filter['store_id'] = $store_ids;
//                } else {
//                    $filter['store_id'] = 0;
//                }
//
//            } else {
//                $filter['store_id'] = 0;
//            }
//        }
    
        if ($test != 1) {
            $filter['is_enable'] = 1;
        }
    
        if (isset($search_filter['user_name']) && $search_filter['user_name']) {
            $user_ids = user_helper::get_user_ids_by_mobile($search_filter['user_name']);
            $filter['user_id'] = $user_ids;
        }
    
        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $filter['add_time >='] = $search_filter['start_date'].' 00:00:00';
        }
        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $filter['add_time <='] = $search_filter['end_date'].' 23:59:59';
        }
        if (isset($progress) && $progress != '-1') {
            $filter['progress'] = intval($progress);
        }
    
        if ($status != -1) {
            $filter['status'] = $status;
        }
    
        // 代理
        if ($task_user_id == 1) {
            $filter['task_user_id > '] = 0;
        } else if ($task_user_id == 0) {
            $filter['task_user_id'] = 0;
        }
    
        if ($progress != '-1' || $status == 0) {
            $filter['status'] = $status;
        } else {
            $filter['1'] = 1;
        }
        // added type filter by ninghx 用来区分总部订单、合伙人订单还是影院订单
        if (isset($type) && $type) {
            $filter['type'] = $type;
        }
    
        $order_list = array();
        if (!$is_empty) {
            $order_count = _model('order')->getTotal($filter);
            if ($order_count) {
                $pager = new Pager($this->per_page);
                if ($pager->generate($order_count)) {
                    Response::assign('pager', $pager);
                }
                $order_by = ' ORDER BY `id` DESC ';
                if(!$is_excel) {
                    $order_by .= $pager->getLimit($page);
                }
                $order_list = _model('order')->getList($filter, $order_by);
                foreach ($order_list as $k=>$v) {
                    $order_list[$k]['order_relation'] = _model('order_relation')->getList(array(
                        'order_id'=>$v['id'],
                        'status'  => 1
                    ));
                }
    
                if($is_excel) {
                    $this->export_excel($order_list);
                }
                Response::assign('count', $order_count);
            }
        }
    
        $express_list = _model('express')->getList(array('status'=>1));
    
        Response::assign('role_id', $role_id);
        Response::assign('express_list', $express_list);
        Response::assign('order_list', $order_list);
        Response::assign('status', $status);
        Response::assign('progress', $progress);
        Response::assign('task_user_id', $task_user_id);
    
        Response::assign('type', $type);
        Response::assign('store_type', $store_type);
        Response::assign('search_filter', $search_filter);
    
        Response::display('admin/movie_store_order_list.html');
    }
}

?>